package com.zhonglz.support.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.zhonglz.model.BuSeal;
import com.zhonglz.model.SysFileInfo;
import com.zhonglz.service.BuSealService;
import com.zhonglz.service.SysFileInfoService;
import com.zhonglz.support.event.SealSignDataEvent;
import com.zhonglz.support.model.SealSignData;
import com.zhonglz.util.minio.MinioUtils;
import com.zhonglz.util.tools.ObjectUtils;

/***
 * 印章处理监听
 * @author zhonglz
 *
 */
@Component
public class SealInfoListener {
	
	private static Logger logger = LoggerFactory.getLogger(SealInfoListener.class);
	
	@Autowired
	private SysFileInfoService sysFileInfoService;
	
	@Autowired
	private BuSealService buSealService;
	
	@Order(20)
    @EventListener
    public void acceptEvent(SealSignDataEvent event) {
		logger.info("***************印章处理监听开始********************");
		SealSignData sealSignData =event.getSealSignData();
		BuSeal buSeal =buSealService.selectById(sealSignData.getBuSignInfo().getSealId());
		SysFileInfo sysFileInfo =sysFileInfoService.selectByPrimaryKey(buSeal.getSealFile());
		if(ObjectUtils.isEmpty(sysFileInfo)) {
			throw new RuntimeException("印章文件信息获取失败");
		}
		try {
			sealSignData.setSealFile(MinioUtils.getObjectBytes(sysFileInfo.getFileAddress()));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("印章文件获取失败");
		}
		event.setSealSignData(sealSignData);
		logger.info("***************印章处理监听结束********************");
	}
}
